/*
 * File: BuildNewTrip.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package triperest.servlets;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import triperest.Trip;
import triperest.TriperestUser;

/**
 * The Class BuildNewTrip.
 */
public class BuildNewTrip extends HttpServlet
{

    /*
     * (non-Javadoc)
     * 
     * @see
     * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
     * , javax.servlet.http.HttpServletResponse)
     */
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException
    {
        resp.setContentType("text/plain");
        Trip trip = new Trip();
        trip.setName(req.getParameter("tripName"));
        trip.save();
        TriperestUser user = new TriperestUser(req);
        user.addTrip(trip);
        user.save();
        if (user.isTemp())
        {
            resp.addCookie(new Cookie("user", user.getId()));
        }
        resp.sendRedirect("/edit.jsp?trip=" + trip.getId());
    }

}
